<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Operationtest\Classe\Table;
use Operationtest\Classe\ExcelManager;

class BudgetController extends AbstractActionController
{
    protected $saleheadTable;
    protected $storeTable;
    protected $marcheTable;
    protected $marcheStoreTable;
    protected $budgetTable;
    protected $siteTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function getSaleheadTable()
    {
        if (!$this->saleheadTable) {
            $sm = $this->getServiceLocator();
            $this->saleheadTable = $sm->get('Operationtest\Model\SaleheadTable');
        }
        return $this->saleheadTable;
    }
    
    public function getStoreTable()
    {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Operationtest\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function getMarchestoreTable()
    {
        if (!$this->marcheStoreTable) {
            $sm = $this->getServiceLocator();
            $this->marcheStoreTable = $sm->get('Operationtest\Model\MarchestoreTable');
        }
        return $this->marcheStoreTable;
    }
    
    public function getMarcheTable()
    {
        if (!$this->marcheTable) {
            $sm = $this->getServiceLocator();
            $this->marcheTable = $sm->get('Operationtest\Model\MarcheTable');
        }
        return $this->marcheTable;
    }
    
    public function getBudgetTable() {
        if (!$this->budgetTable) {
            $sm = $this->getServiceLocator();
            $this->budgetTable = $sm->get('Operationtest\Model\BudgetTable');
        }        
        return $this->budgetTable;
    }
    
    public function getSiteTable() {
        if (!$this->siteTable) {
            $sm = $this->getServiceLocator();
            $this->siteTable = $sm->get('RHX\Model\SiteTable');
        }
        return $this->siteTable;
    }
    
    public function indexAction() { 
        return new ViewModel(); 
    } 
    
    public function getmonthbudgetAction() {
        $request = $this->getRequest();
        if($request->isPost()) { 
            $monthBudget = array();
            foreach($this->getBudgetTable()->getMonthBudget($request->getPost()->month) as $budget) {
                $monthBudget[$budget->idStore]['month'] = $budget->month;
                $monthBudget[$budget->idStore]['nbWeek1'] = $budget->nbWeek1;
                $monthBudget[$budget->idStore]['nbWeek2'] = $budget->nbWeek2;
                $monthBudget[$budget->idStore]['nbWeek3'] = $budget->nbWeek3;
                $monthBudget[$budget->idStore]['nbWeek4'] = $budget->nbWeek4;
                $monthBudget[$budget->idStore]['nbWeek5'] = $budget->nbWeek5;
                $monthBudget[$budget->idStore]['nbWeek6'] = $budget->nbWeek6;
                $monthBudget[$budget->idStore]['totttc'] = $budget->totttc;
                $monthBudget[$budget->idStore]['tm'] = $budget->tm;
                $monthBudget[$budget->idStore]['txFpR'] = $budget->txFpR;
                $monthBudget[$budget->idStore]['txPc'] = $budget->txPc;
                $monthBudget[$budget->idStore]['txRp'] = $budget->txRp;
                $monthBudget[$budget->idStore]['txA'] = $budget->txA;
                $monthBudget[$budget->idStore]['txAl'] = $budget->txAl;
                $monthBudget[$budget->idStore]['txE'] = $budget->txE;
            }
            $variables = array(
                'month' => $request->getPost()->month,
                'sites' => $this->getSiteTable()->getAll(),
                'monthBudget' => $monthBudget,
            );
            
            $viewModel = new viewModel();
            $viewModel->setVariables($variables);
            $viewModel->setTerminal(true);

            return $viewModel;
        }
    }
    
    public function saveAction() { 
        $request = $this->getRequest();
        
        if ($request->isPost()) {            
            $pk = $request->getPost()->pk;
            $name = $request->getPost()->name;
            $value = $request->getPost()->value;
            
            $return = $this->getBudgetTable()->saveBudget($pk, $name, $value);
        }
        
        return new JsonModel(array('return' => $return));
    }
    
    public function importAction() {
        $file = $this->params()->fromRoute('file');
        $sheetnum = $this->params()->fromRoute('sheet');
        if($file) {
            $table = new Table($sm = $this->getServiceLocator());
            $excelManager = new ExcelManager();
            $budgetFile = $excelManager->loadBudgetFile($file.'.xls');
            $sheet = $budgetFile->getSheet($sheetnum);
            
            $datas = array();
            $tablines = array(11,12,13,14,15,16,17,21,46,47,48,49,50,52);
            $tabcolumns = array(3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28);            
            $line = 1;
            foreach($sheet->getRowIterator() as $row) {
                if(in_array($line, $tablines)) {
                    switch ($line) {
                        case 11:
                            $rubrique = 'nbWeek1';
                            break;
                        case 12:
                            $rubrique = 'nbWeek2';
                            break;
                        case 13:
                            $rubrique = 'nbWeek3';
                            break;
                        case 14:
                            $rubrique = 'nbWeek4';
                            break;
                        case 15:
                            $rubrique = 'nbWeek5';
                            break;
                        case 16:
                            $rubrique = 'nbWeek6';
                            break;
                        case 17:
                            $rubrique = 'totttc';
                            break;
                        case 21:
                            $rubrique = 'tm';
                            break;
                        case 46:
                            $rubrique = 'txFpR';
                            break;
                        case 47:
                            $rubrique = 'txPc';
                            break;
                        case 48:
                            $rubrique = 'txRp';
                            break;
                        case 49:
                            $rubrique = 'txA';
                            break;
                        case 50:
                            $rubrique = 'txAl';
                            break;
                        case 52:
                            $rubrique = 'txE';
                            break;
                    }
                    $column = 1;
                    foreach($row->getCellIterator() as $cell) {
                        if(in_array($column, $tabcolumns)) {
                            switch ($column) {
                                case 3:
                                    $idsite = intval($column) - 2;
                                    break;
                                default:
                                    $idsite = intval($column) - 1;
                            }
                            $value = ($cell->getCalculatedValue())?$cell->getCalculatedValue():0;
                            $value = str_replace(' ', 0, $value);
                            $value = str_replace('#DIV/0!', 0, $value);
                            $datas[$idsite][$rubrique] = $value;
                        }
                        $column++;
                    }
                }
                $line++;
            } 
            
            $year = substr($file, -4);
            switch ($sheetnum) {
                case 0:
                    $month = '01';
                    break;
                case 1:
                    $month = '02';
                    break;
                case 2:
                    $month = '03';
                    break;
                case 3:
                    $month = '04';
                    break;
                case 4:
                    $month = '08';
                    break;
                case 5:
                    $month = '06';
                    break;
                case 6:
                    $month = '07';
                    break;
                case 7:
                    $month = '08';
                    break;
                case 8:
                    $month = '09';
                    break;
                case 9:
                    $month = '10';
                    break;
                case 10:
                    $month = '11';
                    break;
                case 11:
                    $month = '12';
                    break;
            }
            
            $date = $year.'-'.$month.'-01';
            foreach($datas as $idStore => $data) {
                $table->getBudgetTable()->importBudget($idStore, $date, $data);
            }
        }
    }
    
    
    
    public function echoShortDayText() {
        $dayText[0] = 'dim';
        $dayText[1] = 'lun';
        $dayText[2] = 'mar';
        $dayText[3] = 'mer';
        $dayText[4] = 'jeu';
        $dayText[5] = 'ven';
        $dayText[6] = 'sam';        
        
        return $dayText;
    }
    
    public function echoLongDayText() {
        $dayText[0] = 'dimanche';
        $dayText[1] = 'lundi';
        $dayText[2] = 'mardi';
        $dayText[3] = 'mercredi';
        $dayText[4] = 'jeudi';
        $dayText[5] = 'vendredi';
        $dayText[6] = 'samedi';        
        
        return $dayText;
    }
    
    public function echoMonthText() {

        $monthText[1] = 'Janvier';
        $monthText[2] = 'Fevrier';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        return $monthText;
    }
}
?>
